package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.MessageEntity;
import com.net.tkc.pro.entity.Post;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.service.PostService;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Post/")
public class PostController {
    ResultEntity resultEntity = null;
    @Resource
    PostService postService;


    @PostMapping("addPost")
    private String addPost(
            @RequestParam("userid") int userid,
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("picture") String picture,
            @RequestParam("video") String video,
            @RequestParam("type") String type
    ){
        resultEntity = new ResultEntity();
        Post post=new Post();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=new Date();
        String Time=dateFormat.format(date);
        post.setDtcreate(Time);
        post.setUserid(userid);
        post.setTitle(title);
        post.setcontent(content);
        post.setPicture(picture);
        post.setVideo(video);
        post.setType(type);
        try{
            int result = postService.addPost(post);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 获取帖子类型的多表连查
     * @param type
     * @param limit
     * @param page
     * @return
     */
    @GetMapping("PostList2")
    private String getPostList2(
            @RequestParam("type") String type,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        resultEntity = new ResultEntity();
        try{
            int Start =(page-1)*limit;
            boolean flag=false;
            if(!type.equals("") || type !=null){
                flag=true;
            }
            if(!type.equals("") || type !=null){
                type="%"+type+"%";
            }
            List<Post> list =postService.getPostList2(type,Start,limit);
            if(flag){
                resultEntity.setCount(list.size());
            }else{
                resultEntity.setCount(postService.getPost());
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("获取成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据类型查找帖子
     * @param type 类型
     * @param limit 一页几条
     * @param page 第几页
     * @return
     */

    @GetMapping("PostList")
    private String getPostList(
            @RequestParam("type") String type,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        resultEntity = new ResultEntity();
        try{
            int Start =(page-1)*limit;
            boolean flag=false;
            if(!type.equals("") || type !=null){
                flag=true;
            }
            if(!type.equals("") || type !=null){
                type="%"+type+"%";
            }
            List<Post> list =postService.getPostList(type,limit,Start);
            if(flag){
                resultEntity.setCount(list.size());
            }else{
                resultEntity.setCount(postService.getPost());
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("获取成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 无条件查询Post里面的数据
     * @return
     */
    @GetMapping("Post")
    private String getPost(
    ){
        resultEntity = new ResultEntity();
        try{
            List<Post> list =postService.getPost();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 更新类型和封禁
     * @param id
     * @return
     */
    @PutMapping("Type")
    private String upTyoe(
            @RequestParam("type") String type,
            @RequestParam("ban") int ban,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upType(type, ban, id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("失败！请联系管理员");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     *  根据id更新评论统计次数
     * @param id
     * @return
     */

    @PutMapping("Ban")
    private String upban(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upBan(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("封禁成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("失败！请联系管理员");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据id更新评论统计次数
     * @param id
     * @return
     */

    @PutMapping("Post5")
    private String upPostComment(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upPostComment(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据id更新观看次数
     * @param id
     * @return
     */

    @PutMapping("Post4")
    private String upPostCount_see(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upPostCount_see(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据id更新分享次数
     * @param id
     * @return
     */

    @PutMapping("Post3")
    private String upPostCount_share(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upPostCount_share(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据id更新点赞数
     * @param id
     * @return
     */

    @PutMapping("Post2")
    private String upPostCount_fabulous(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upPostCount_fabulous(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 更新大部分内容
     * @param id id
     * @param title 标题
     * @param content 内容
     * @param picture 图片地址
     * @param video 视频地址
     * @param type 类型分类
     * @param examine 审核类型
     * @param ban 是否封禁
     * @return json
     */
    @PutMapping("Post")
    private String upPost(
            @RequestParam("id") int id,
            @RequestParam("title") String title,
            @RequestParam("content") String content,
            @RequestParam("picture") String picture,
            @RequestParam("video") String video,
            @RequestParam("type") String type,
            @RequestParam("examine") int examine,
            @RequestParam("ban") int ban
    ){
        resultEntity = new ResultEntity();
        try{
            int result = postService.upPost(title, content, picture, video, type, examine, ban, id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 根据用户id查找自己的帖子
     * @param userid
     * @return
     */
    @GetMapping("Post2")
    private String getPost2(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Post> list =postService.getPost();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**根据帖子id查找帖子
     * @param userid 帖子id
     * @return
     */
    @GetMapping("GetId")
    private String GetId(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Post> list =postService.getId(userid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**根据帖子id查找帖子
     * @param id 帖子id
     * @return
     */
    @GetMapping("GetPostid")
    private String GetPostid(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            List<Post> list =postService.getPostid(id);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
